package com.ruoyi.system.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MapLoad {

    public boolean LoadMap(double lat_up, double lon_up, double lat_down, double lon_down,String map_name){
        String link = "http://webrd04.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}";
        for(int zoom = 1;zoom <= 17;zoom++){
            int c = 0;
            int fail = 0;
            Level level = getTileNumber(lat_up,lon_up,lat_down,lon_down,zoom);
            System.out.println(level.toString());
            //Level level = new Level( 17,25878, 26073, 7360, 7670);
            for (int i = level.getX_min(); i <= level.getX_max(); i++) {   //循环X
                for (int j = level.getY_min(); j <= level.getY_max(); j++) {    //循环Y
                    try {
                        URL url = new URL(link.replace("{x}", i + "").replace("{y}", j + "").replace("{z}", zoom + ""));
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
                        conn.setConnectTimeout(10000);
                        conn.connect();
                        InputStream in = conn.getInputStream();
                        String path = "SystemData/maps/"+map_name+"/";
                        String filePath = new File(path).getAbsolutePath()+"/";
                        File dir = new File(filePath + zoom + "/" + i);
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }
                        File file = new File(filePath + zoom + "/" + i + "/" + j + ".jpg");
                        if (!file.exists()) {
                            file.createNewFile();
                        }
                        OutputStream out = new FileOutputStream(file);
                        byte[] bytes = new byte[1024 * 20];
                        int len = 0;
                        while ((len = in.read(bytes)) != -1) {
                            out.write(bytes, 0, len);
                        }
                        out.close();
                        in.close();
                        System.out.println("已成功下载:" + zoom + "_" + i + "_" + j + ".jpg");
                        c++;
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                        fail++;
                    }
                }    //循环Y结束
            }   //循环X结束
            //Thread.sleep(1000);
            System.out.println("共下载:   " + c + "   张");
            System.out.println("失败:   " + fail + "   张");
        }
        return true;
    }

    public Level getTileNumber(double lat_up, double lon_up, double lat_down, double lon_down, int zoom) {
        //左上
        int xtile_up = (int)Math.floor( (lon_up + 180) / 360 * (1<<zoom) ) ;
        int ytile_up = (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(lat_up)) + 1 / Math.cos(Math.toRadians(lat_up))) / Math.PI) / 2 * (1<<zoom) ) ;
        if (xtile_up < 0)
            xtile_up=0;
        if (xtile_up >= (1<<zoom))
            xtile_up=((1<<zoom)-1);
        if (ytile_up < 0)
            ytile_up=0;
        if (ytile_up >= (1<<zoom))
            ytile_up=((1<<zoom)-1);
        //右下
        int xtile_down = (int)Math.floor( (lon_down + 180) / 360 * (1<<zoom) ) ;
        int ytile_down = (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(lat_down)) + 1 / Math.cos(Math.toRadians(lat_down))) / Math.PI) / 2 * (1<<zoom) ) ;
        if (xtile_down < 0)
            xtile_down=0;
        if (xtile_down >= (1<<zoom))
            xtile_down=((1<<zoom)-1);
        if (ytile_down < 0)
            ytile_down=0;
        if (ytile_down >= (1<<zoom))
            ytile_down=((1<<zoom)-1);
        return new Level(zoom,xtile_down,xtile_up,ytile_up,ytile_down);
    }

    static class Level{
        private int zoom;
        private int x_min;
        private int x_max;
        private int y_min;
        private int y_max;

        Level(int level, int x_min, int x_max, int y_min, int y_max) {
            this.zoom = level;
            this.x_min = x_min;
            this.x_max = x_max;
            this.y_min = y_min;
            this.y_max = y_max;
        }

        public int getZoom() {
            return zoom;
        }

        public int getX_min() {
            return x_min;
        }

        public int getX_max() {
            return x_max;
        }

        public int getY_min() {
            return y_min;
        }

        public int getY_max() {
            return y_max;
        }

        @Override
        public String toString() {
            return "Level{" +
                    "zoom=" + zoom +
                    ", x_min=" + x_min +
                    ", x_max=" + x_max +
                    ", y_min=" + y_min +
                    ", y_max=" + y_max +
                    '}';
        }
    }
}
